package boddaert.philippe.cashGame.net;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

import org.apache.log4j.Logger;

import sun.net.www.protocol.file.SimpleFileURLConnection;

public class FileURLStreamHandler extends URLStreamHandler {

	private final static Logger logger = Logger
			.getLogger(FileURLStreamHandler.class);

	@Override
	protected URLConnection openConnection(URL u) throws IOException {

		String fileNamePath = u.getHost() + "/index.html";

		logger.debug("file to connect to : " + fileNamePath);
		URL systemResource = ClassLoader.getSystemResource("html/"
				+ fileNamePath);

		if (systemResource != null) {
			String path = systemResource.getPath();

			SimpleFileURLConnection file = new SimpleFileURLConnection(u,
					new File(path));

			return file;
		}
		return null;
	}

}
